package bishi.youkushengshi;

import java.util.Scanner;

public class demo3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] prices = new int[n];
        for(int i = 0;i < n;i++) {
            prices[i] = in.nextInt();
        }
        int ret = max(k,prices);
        System.out.println(ret);

    }

    static int max(int k,int[] prices) {
        int n = prices.length;
        //边界值情况
        if(n <= 1|| k == 0) {
            return 0;
        }

        int[][] dp = new int[k+1][2];
        for(int m = 0;m <=k;m++) {
            dp[m][1] = Integer.MIN_VALUE;

        }
        dp[0][1] = - prices[0];

        for(int i = 0;i < n;i++) {
            for(int j = 1;j <= k;j++) {
                dp[j][0] = Math.max(dp[j][0],dp[j][1] + prices[i]);
                dp[j][1] =  Math.max(dp[j][1],dp[j - 1][0] - prices[i]);
            }
            dp[0][1] = Math.max(dp[0][1],-prices[i]);
        }
        return Math.max(dp[k][0],0);
    }
}
